﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ServeNHL.Models
{
    [MetadataType(typeof(ExtUserInfoDetailsMetadata))]
    public partial class EXT_USER_INFO_DETAILS
    {
        public int saveExtUserDetails(UserEventViewModel ext)
        {
            int detID = 0;

            if(ext.extUserInfoDetails.USER_DETAILS_ID >= 0)
            {
                updateExtUserDetails(ext.extUserInfoDetails);
            }
            else
            {
                insertExtUserDetails(ext.extUserInfoDetails);
            }

            detID = ext.extUserInfoDetails.USER_DETAILS_ID;

            return detID;

        }// end of public int saveExtUserDetails(UserEventViewModel ext)

        public int insertExtUserDetails(EXT_USER_INFO_DETAILS extUserdet)
        {
            int extinfoId = 0;
            using (var dbconn = new fclick_dbEntities())
            {

                var tempDet = new EXT_USER_INFO_DETAILS
                {
                    EXT_USER_INFO_ID = extUserdet.EXT_USER_INFO_ID,
                    USER_ID = extUserdet.USER_ID,
                    START_DATE = extUserdet.START_DATE,
                    EXPIRATION_DATE = extUserdet.EXPIRATION_DATE,
                    STATUS=extUserdet.STATUS,
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),
                    CREATED_DATE = Helpers.createDate.getCreateDate()
                };

                dbconn.EXT_USER_INFO_DETAILS.Add(tempDet);
                dbconn.SaveChanges();

                extinfoId = tempDet.USER_DETAILS_ID;
            }//end of using (var dbconn = new fclick_dbEntities())
            return extinfoId;
        }//end of  public int insertExtUserDetails(EXT_USER_INFO_DETAILS extUserdet)

        public int updateExtUserDetails(EXT_USER_INFO_DETAILS extUserdet)
        {
            int detID = 0;

            using(var dbconn = new fclick_dbEntities())
            {
                var tempdet = new EXT_USER_INFO_DETAILS();

                if (extUserdet.EXT_USER_INFO_ID >= 0)
                {
                    tempdet.EXT_USER_INFO_ID = extUserdet.EXT_USER_INFO_ID;
                }

                if (extUserdet.USER_ID >= 0)
                {
                    tempdet.USER_ID = extUserdet.USER_ID;
                }

                if (!String.IsNullOrEmpty(extUserdet.START_DATE.ToString()))
                {
                    tempdet.START_DATE = extUserdet.START_DATE;
                }
                
                if (!String.IsNullOrEmpty(extUserdet.STATUS))
                {
                    tempdet.STATUS = extUserdet.STATUS;
                }

                dbconn.EXT_USER_INFO_DETAILS.Add(tempdet);
                dbconn.SaveChanges();

                detID = tempdet.USER_DETAILS_ID;
            }//end of using(var dbconn = new fclick_dbEntities())
            return detID;
        }

       


    }//end of public class ExtUserEventDetails
}